const app =new Vue({
  el: '#app',
  data: 
  {
    books:
    [
      {
        id: 1,
        name: '《算法导论》',
        date: '2006-9',
        price: 85.00,
        count: 1
      },
      {
        id: 2,
        name: '《UNIX编程艺术》',
        date: '2006-2',
        price: 59.00,
        count: 1
      },
      {
        id: 3,
        name: '《编程珠玑》',
        date: '2006-9',
        price: 85.00,
        count: 1
      },
      {
        id: 4,
        name: '《Python学习大全》',
        date: '2002-1',
        price: 185.00,
        count: 1
      },
      {
        id: 5,
        name: '《C语言程序设计》',
        date: '2002-9',
        price: 85.00,
        count: 1
      },
    ]
  },
  methods:
  {
    getfinalPrice(price)
    {
      // 保留两位小数返回
      return '￥' + price.toFixed(2);
    },

    increment(index)
    {
      this.books[index].count++;
      console.log('increment');
    },
    decrement(index)
    {
      this.books[index].count--;
      console.log('decrement');
    },
    removehandle(index)
    {
      this.books.splice(index, 1);
    }
  },
  filters: 
  {
    // 过滤器事件
    showPrice(price)
    {
      return '￥' + price.toFixed(2);
    }  
  },
  computed:
  {
    totalPrice()
    {
      // 1. 普通for循环遍历
      let totalPrices = 0; 
      // for (let i = 0; i < this.books.length; i++)
      // {
      //   totalPrices += this.books[i].price * this.books[i].count;
      // }  
      // return totalPrices;

      // 2. for (let i in this.books)
      // for (let i in this.books)
      // {
      //   const book = this.books[i];
      //   totalPrices += book.price * book.count;
      // }


      // 3. for (let i of this.books) 直接拿到指定对象
      // for (let item of this.books)
      // {
      //   totalPrices += item.price * item.count;
      // }

      return this.books.reduce(function (preValue,book)
      {
        return preValue + book.price * book.count;
      }, 0);
    }
  }
});